package gui.util;

import gui.util.event.FileTreeNodeMouseListener;
import gui.util.event.TreeCheckSelectionListener;

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

public class CheckboxedTree extends JTree
{
	private static final long serialVersionUID = 1L;
	private Collection<File> filesCollection;

	public Collection<Object> getSelectedObjects(){
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	public void setSelectedObject(Object o){
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	public void setSelectedObjects(Collection<Object> objects){
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	public void selectAllObjects(){
		throw new UnsupportedOperationException("Not yet implemented.");
	}
	
	//This is IMHO JTree's method for clearing selection of item in general term
	//Not in terms of unchecking FileTreeNode(s) and therefore should not be
	//used, because we don't know what method's are calling this method.
	//we might for example experience unchecking of all nodes by clicking to a
	//whitespace area of the CheckboxedTree
//	public void clearSelection(){
//		throw new UnsupportedOperationException("Not yet implemented.");
//	}
	
	public void addTreeCheckSelectionListener(
				TreeCheckSelectionListener listener){
		
	}
	
	public void removeTreeCheckSelectionListener(
				TreeCheckSelectionListener listener){
		
	}
	
	public CheckboxedTree(TreeNode root) {
		super(root);
	}
	
	public Collection<File> getSelectedFiles() {
		
		filesCollection = new ArrayList<File>();
		
		FileTreeNode root = (FileTreeNode)this.getModel().getRoot();
		if (root.getFileCheckBox().isSelected()) {
			recursion(root);
		}

		
		return filesCollection;
	}
	
	//This method should be renamed to better express its purpose
	//It recursively takes all children of parentNode and those which are
	//checked(FileCheckBox) are added to filesCollection attribute
	private void recursion(FileTreeNode parentNode) {
		
		Enumeration<FileTreeNode> e = parentNode.children();
		FileTreeNode node;
		while (e.hasMoreElements()) {
			node = (FileTreeNode)e.nextElement();
			if (node.getFileCheckBox().isSelected()) {
				filesCollection.add(node.getFileCheckBox().getFile());
				recursion(node);
			}
			
		}
		
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Checkboxed Tree Test");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());

		FileTreeNode root = new FileTreeNode(new File("C:\\"));
		
		CheckboxedTree tree = new CheckboxedTree(root);

		tree.setCellRenderer(new FileTreeNodeCellRenderer());
		tree.addMouseListener(new FileTreeNodeMouseListener());
		
		frame.add(new JScrollPane(tree), BorderLayout.WEST);
		
		final CheckboxedTree t = tree;
		
		JButton button = new JButton("Print Selected");
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				for (File file : t.getSelectedFiles()) {
					System.out.println(file.getName());
				}
				
			}
			
		});
		
		frame.add(button, BorderLayout.EAST);
				
		frame.pack();
		frame.setVisible(true);
	}
	
}
